<template xmlns:v-slot="http://www.w3.org/1999/XSL/Transform">
  <wx-tables
    :columns="columns"
    title="金刚区管理"
    :data-source="dataSource"
    :loading="loading"
    :total="total"
    :size.sync="size"
    :page.sync="page"
    @handlePage="handlePage"
  >
    <!--      <template #search>-->
    <!--        &lt;!&ndash;        <tip-block tip="搜索" />&ndash;&gt;-->
    <!--        <el-form label-position="left" style="margin-top: 10px;display: flex;flex-wrap:wrap;justify-content: space-between">-->
    <!--          <el-form-item style="display: flex;" label="学生姓名" prop="search_code">-->
    <!--            <el-input v-model="searchForm.search_words" clearable placeholder="请输入" />-->
    <!--          </el-form-item>-->
    <!--          <el-form-item style="display: flex;" label="请假单状态" prop="search_code">-->
    <!--            <el-select v-model="searchForm.leave_status_str" clearable placeholder="请选择">-->
    <!--              <el-option-->
    <!--                v-for="(item, type) of LEAVE_STATUS_LIST"-->
    <!--                :key="type"-->
    <!--                :label="item.title"-->
    <!--                :value="item.value"-->
    <!--              />-->
    <!--            </el-select>-->
    <!--          </el-form-item>-->
    <!--          <el-form-item style="display: flex;" label="所在学部" prop="search_code">-->
    <!--            <el-select v-model="searchForm.year_department_id" clearable placeholder="请选择学部">-->
    <!--              <el-option-->
    <!--                v-for="item in select_dept"-->
    <!--                :key="item.type"-->
    <!--                :label="item.label"-->
    <!--                :value="item.type"-->
    <!--              />-->
    <!--            </el-select>-->
    <!--          </el-form-item>-->
    <!--          <el-form-item style="display: flex;" label="所在年级" prop="search_code">-->
    <!--            <el-select v-model="searchForm.search_grade_name" clearable placeholder="请选择年级">-->
    <!--              <el-option-->
    <!--                v-for="item of 20"-->
    <!--                :key="item"-->
    <!--                :label="item"-->
    <!--                :value="item"-->
    <!--              />-->
    <!--            </el-select>-->
    <!--          </el-form-item>-->
    <!--          <el-form-item style="display: flex;" label="所在班级" prop="search_code">-->
    <!--            <el-select v-model="searchForm.search_class_name" clearable placeholder="请选择班级">-->
    <!--              <el-option-->
    <!--                v-for="item of 20"-->
    <!--                :key="item"-->
    <!--                :label="item"-->
    <!--                :value="item"-->
    <!--              />-->
    <!--            </el-select>-->
    <!--          </el-form-item>-->
    <!--          <div>-->
    <!--            <el-button type="primary" class="table-btn" icon="el-icon-search" @click="handleSearchClick">筛选</el-button>-->
    <!--            <el-button class="table-btn" icon="el-icon-refresh" @click="handleRectSearch">重置</el-button>-->
    <!--          </div>-->
    <!--        </el-form>-->
    <!--      </template>-->

    <!-- tools -->
    <template #tools>
<!--      <el-button type="primary" class="table-btn" @click="handleOpenForm()">新增金刚区</el-button>-->
      <!--      <el-button class="table-btn" icon="el-icon-refresh-right" @click="handleGetTable">刷新</el-button>-->
    </template>
    <template v-slot:img="record">
      <span v-if="!record.row.img">-</span>
      <el-avatar v-else shape="square" :size="100" fit="cover" :src="record.row.img"></el-avatar>
    </template>
    <template v-slot:status="record">
      <el-switch
        v-model="record.row.status"
        :active-value="1"
        :inactive-value="0"
        active-color="#409EFF"
        inactive-color="#C0CCDA"
        @change="handleSetStatus(record.row)">
      </el-switch>
    </template>
    <template v-slot:oper="record">
      <div class="oper-box">
        <span><el-link type="primary" @click="handleEit(record.row)">编辑</el-link></span>
<!--        <el-popconfirm-->
<!--          title="确定删除当前账号吗？"-->
<!--          @confirm="handleDelete(record.row.id)"-->
<!--        >-->
<!--          <el-link slot="reference" type="danger">删除</el-link>-->
<!--        </el-popconfirm>-->
      </div>
    </template>
    <wx-dialog
      ref="wx-dialog"
      :visible.sync="formVisible"
      :is-add="isAdd"
      width="600px"
      @ok="handleSaveForm"
    >
      <div class="wx-dialog-form">
        <el-form ref="ruleForm" v-loading="formLoading" :rules="ruleForm" :model="formInfo" label-position="left" label-width="90px">
          <el-form-item label="图标名称" prop="name">
            <el-input v-model="formInfo.name" style="width: 200px;" placeholder="请输入图片名称" />
          </el-form-item>
          <el-form-item label="上传图片" prop="img">
            <el-upload
              class="up-load"
              :class="formInfo.img && formInfo.img.length > 0 ? 'hidden':''"
              action="https://up.qiniup.com"
              :limit="1"
              :data="uploadData"
              accept="image/*"
              :file-list="formInfo.img"
              list-type="picture-card"
              :before-upload="beforeAvatarUpload"
              :on-success="upSuccess"
              :on-exceed="overMax"
              :on-remove="handleRemove"
            >
              <i slot="default" class="el-icon-plus"></i>
            </el-upload>
          </el-form-item>
        </el-form>
      </div>
    </wx-dialog>
  </wx-tables>
</template>

<script>
import WxDialog from '@/components/WxDialog'
import WxTables from '@/components/WxTables'
import TableMixins from '@/components/WxTables/mixins'
import { BANNER_POSITION } from '@/views/constants'
import { sysNavigationEdit, sysNavigationGetListByCon } from '@/api/district'
export default {
  name: 'LatestInformation',
  components: {
    WxDialog,
    WxTables
  },
  mixins: [TableMixins],
  data() {
    return {
      pwdDialog: false,
      columns: [
        { config: { type: 'index', width: 100 }, label: '序号' },
        { dataIndex: 'name', label: '图标名称'},
        { dataIndex: 'img', label: '图片', slots: true },
        { dataIndex: 'created_at', label: '创建时间' },
        // { dataIndex: 'status', label: '是否启用', slots: true },
        { dataIndex: 'oper', label: '操作', slots: true }
      ],
      roleList: [],
      url_type:'1',
      markId: '',
      BANNER_POSITION,
      ruleForm: {
        name: [
          { required: true, message: '请输入图标名称', trigger: 'blur' }
        ],
        img: [
          { required: true, message: '请上传图片', trigger: 'blur' }
        ]
      },
    }
  },
  methods: {
    /**
     * 获取列表
     */
    getList() {
      sysNavigationGetListByCon( {town_id:this.town_id,page: this.page}).then(res => {
        if (this.pagination) {
          const { data, total } = res.data
          this.total = total
          this.dataSource = data
          this.cacheDataSource = JSON.parse(JSON.stringify(data))
        } else {
          this.dataSource = res.data
          this.cacheDataSource = JSON.parse(JSON.stringify(res.data))
        }
        this.handleSetCancelStatus?.()
        // this.handleCompleteTable()
      }).finally(() => {
        this.loading = false
      })
    },
    /**
     * 新增/编辑人员
     */
    createManager(idSend) {
      const { img, name } = this.formInfo
      let from_info = { name }
      from_info.img = img[0].name ? this.img_url + img[0].response.key : this.img_url + img[0].uid
      if(Object.keys(idSend).length) {
        sysNavigationEdit({ ...from_info,...idSend,town_id: this.town_id })
          .then((res) => {
            if (res.code === 200) {
              this.$message.success(this.isAdd ? '添加成功' : '编辑成功')
              this.formInfo = {}
              this.formVisible = false
              this.handleGetTable()
            }
          }).finally(() => {
          this.handleResetDialog()
        })
      }else{
        sysBannerCreate({ ...from_info })
          .then((res) => {
            if (res.code === 200) {
              this.$message.success(this.isAdd ? '添加成功' : '编辑成功')
              this.formInfo = {}
              this.formVisible = false
              this.handleGetTable()
            }
          }).finally(() => {
          this.handleResetDialog()
        })
      }
    },
    /**
     * 编辑
     * @param item
     */
    handleEit(item) {
      let { id, name, img } = item
      img = img ? [{ url: img, uid: img.substring(img.lastIndexOf('/')+1) }] : []
      this.markId = 'sys_navigation_id'
      this.handleOpenForm(id,['name','img'],{name,img})
    },
    /**
     * 删除
     * @param admin_ids
     */
    handleDelete(sys_banner_ids) {
      sysBannerDestroy({ sys_banner_ids }).then((res) => { if (res.code === 200) {
        this.$message.success(res.msg)
        this.handleGetTable()
      } })
    },
    /**
     * 监控上传数量
     */
    overMax() {
      this.$message.warning({ message: '上传图片最大为1张' })
    },
    /**
     * 得到上传成功的文件
     * @param fileList
     * @param type
     */
    getFile(fileList,type) {
      this.$set(this.formInfo,'img',fileList)
    },
    /**
     * 删除图片
     * @param fileList
     */
    deleteFile(fileList) {
      this.$set(this.formInfo,'img',fileList)
    },
    /**
     * 变化清空
     */
    handleEmpty() {
      this.$set(this.formInfo,'target','')
    },
    /**
     * 翻页
     * @param val
     */
    handlePage(val) {
      this.page = val
    }
  }
}
</script>
<style lang="scss" src="./style/index.scss" scoped/>
<style scoped>
.m-l-16 {
  margin-left: 16px;
}
.up-load {
  height: 174px;
}
.hidden >>> .el-upload--picture-card {
  display: none;
}
::v-deep .el-avatar>img {
  width: 100%;
}
</style>
